<template>
  <a-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-model-item label="所属基地" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select  v-decorator="['parentId', validatorRules.parentId]" placeholder="请选择所属基地" :getPopupContainer= "(target) => target.parentNode">
            <a-select-option v-for="d in baseList" :key="d.id" :value="d.id">{{ d.name }}</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-item label="大棚名字" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name', validatorRules.name]" placeholder="请输入大棚名字"></a-input>
        </a-form-item>
        <a-form-item label="大棚编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['number', validatorRules.number]" placeholder="请输入大棚编号" style="width: 100%"/>
        </a-form-item>

        <a-form-item label="种植分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['classify', validatorRules.classify]" :trigger-change="true" dictCode="planting_classify" placeholder="请选择采收分类"/>
        </a-form-item>
        <a-form-item label="种植产品" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-multi-select-tag
            v-model="model.variety"
            v-decorator="['variety', validatorRules.variety]"
            :options="varietyList"
            placeholder="请选择种植产品">
          </j-multi-select-tag>
        </a-form-item>

        <a-form-item label="负责人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['manager', validatorRules.manager]" placeholder="请输入负责人"></a-input>
        </a-form-item>
        <a-form-item label="联系手机" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['phone', validatorRules.phone]" placeholder="请输入联系手机"></a-input>
        </a-form-item>
        <a-form-item label="大棚面积" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['aera']" placeholder="请输入大棚面积" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="纬度" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['lat']" placeholder="请输入纬度" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="经度" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['lng']" placeholder="请输入经度" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="海拔" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['altitude']" placeholder="请输入海拔" style="width: 100%"/>
        </a-form-item>


      </a-form>
    </a-spin>
    <a-button type="primary" @click="handleOk">确定</a-button>
    <a-button type="primary" @click="handleCancel">取消</a-button>
  </a-drawer>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import { queryFlowerBase, ajaxGetDictItems,getVarietyNameList , } from '@/api/api'
  export default {
    name: "FlowerPlantingGreenhouseModal",
    components: {
      JDate,
    },
    data () {
      return {
        baseList: [], //基地信息
        classifyList: [], //种植分类数据字典
        varietyList: [], //种植产品数据字典
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          classify: {
            rules: [
              { required: true, message: '请选择种植分类!'},
            ]
          },
          variety: {
            rules: [
              { required: true, message: '请选择种植产品!'},
            ]
          },
          name: {
            rules: [
              { required: true, message: '请输入大棚名字!'},
            ]
          },
          number: {
            rules: [
              { required: true, message: '请输入大棚编号!'},
            ]
          },
          manager: {
            rules: [
              { required: true, message: '请输入负责人!'},
            ]
          },
          phone: {
            rules: [
              { required: true, message: '请输入联系手机!'},
            ]
          },
          parentId: {
            rules: [
              { required: true, message: '请选择基地!'},
            ]
          },
        },
        url: {
          add: "/flower/flowerPlantingGreenhouse/add",
          edit: "/flower/flowerPlantingGreenhouse/edit",
        }
      }
    },
    created () {
      this.initBaseInfo()
      this.initClassifyDict()
      // this.initVarietyDict()
      this.initVarietyNameList()
    },

    methods: {
      //初始化基地信息
      initBaseInfo(){
        queryFlowerBase().then((res)=>{
          if(res.success){
            this.baseList = res.result;
          }
        });
      },
      initClassifyDict(){
        const dictCode = 'planting_classify'
        ajaxGetDictItems(dictCode, null).then((res) => {
          if (res.success) {
            this.classifyList = res.result;
          }
        })
      },
      initVarietyDict(){
        // const dictCode = 'planting_variety'
        // ajaxGetDictItems(dictCode, null).then((res) => {
        //   if (res.success) {
        //     this.varietyList = res.result;
        //   }
        // })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'createTime','updateTime','createUser','updateUser','name','number','manager','phone','aera','lat','lng','altitude','parentId','classify','variety'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'createTime','updateTime','createUser','updateUser','name','number','manager','phone','aera','lat','lng','altitude','parentId','classify','variety'))
      } ,
      initVarietyNameList() {
        const that = this
        getVarietyNameList().then((res)=>{
          if(res.success){
            // that.varietyNameList = res.result
            this.varietyList = res.result.map((item,index,arr)=>{
              let c = {label:item.name, value:item.name}
              return c;
            })
          }else{
            that.$message.warning(res.message)
          }
        });
      }

    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>